package cn.lanqiao.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.lanqiao.dao.TeacherDao;
import cn.lanqiao.util.JdbcUtil;

public class TeacherDaoImpl implements TeacherDao {
	
	//获取教师所有信息
	@Override
	public Object[][] getAllteacher() {
		Connection conn = JdbcUtil.getConnection();
		PreparedStatement ps =null;
		ResultSet rs = null;
		int count = this.count();
		Object[][] datas = new Object[ count][];
		String sql ="select * from EDU_TEACHER";
		
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			for(int i = 0 ;rs.next();i++ ) {
				datas[i]= new Object[] {
					rs.getString("tid"),rs.getString("tname")	
				};
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtil.close(rs, ps, conn);
		}
		return datas;
	}

	//获取教师表行数
	@Override
	public int count() {
		Connection conn = JdbcUtil.getConnection();
		PreparedStatement ps =null;
		ResultSet rs = null;
		String sql ="select count(*) from EDU_TEACHER";
		int num = 0;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if(rs.next()) {
				num = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtil.close(rs, ps, conn);
		}
		return num;
	}

}
